﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebPattern.Core;
using WebPattern.DataAccess;
using WebPattern.MVC.Models;
using WebPattern.Core.Logger;
using WebPattern.Core.Security;

namespace WebPattern.MVC.Controllers
{
    public class TestController : BaseController
    {
        [NHibernateSession]
        public ActionResult Index()
        {

            #region Throw excetptions
            
            // The exception that is thrown when one of the arguments provided to a method is not valid
            throw new ArgumentException("Number must be even", "num"); // num is name of methotd argument
            throw new ArgumentNullException("value"); // value is name of methotd argument
            throw new ArgumentOutOfRangeException("age", "All guests must be 21-years-old or older."); // age is name of methotd argument

            // The exception that is thrown when an attempt is made to access an element of an array with an index that is outside the bounds of the array. This class cannot be inherited.
            throw new IndexOutOfRangeException("TestList");

            // The exception that is thrown when a requested method or operation is not implemented.
            throw new NotImplementedException("");

            // The exception that is thrown when there is an attempt to dereference a null object reference.
            throw new NullReferenceException("test");

            // The exception that is thrown for invalid casting or explicit conversion.
            throw new InvalidCastException("");

            // The exception that is thrown when authentication fails for an authentication stream.
            throw new System.Security.Authentication.AuthenticationException();

            // CryptographicException Class
            throw new System.Security.Cryptography.CryptographicUnexpectedOperationException();

            // The exception that is thrown when the time allotted for a process or operation has expired.
            throw new TimeoutException("The operation has timed-out.");

            throw new Exception("test");
            #endregion

            string a = Resources.Views.Test.IndexRes.CultureName;

            #region Nhibernate Select
            TestRepository tr = new TestRepository();
            Test t = tr.GetById(1);

            //Test newTest = new Test()
            //{
            //    Activ = true,
            //    Description = "dd",
            //    Name = "test2"
            //};
            //try
            //{
            //    tr.Save(newTest);
            //    tr.Flush();
            //}
            //catch
            //{ }
            #endregion

            #region Nhibernate Create
            //User _user = new User()
            //{
            //    UserName = "username",
            //    Email = "email",
            //    ApplicationName = AppName.Mvc,
            //    Password = "dd",
            //    PasswordQuestion = "ddd",
            //    PasswordAnswer = "ddd",//SecurityHelper.PasswordEncrypt(passwordAnswer, username),
            //    IsApproved = true,
            //};

            //try
            //{
            //    UserRepository uRep = new UserRepository();
            //    uRep.Save(_user);
            //    uRep.Flush();
            //}
            //catch (Exception ex)
            //{
            //    throw new MemberAccessException("Error processing membership data", ex);
            //}
            #endregion

            #region Error logging tests
            //Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception());
            CLogger.Audit(Operation.Add, "OK");
            Logger.Error("fake error", new Exception());
            //throw new Exception();
            #endregion

            return View(new TestModels { ExemplarTest = t, Text = a });
            //{
            //    ExemplarTest = new Test()
            //                    {
            //                        Id = 1,
            //                        Name = "dd",
            //                        Activ = true,
            //                        Description = "desctription"
            //                    },
            //    Text = a
            //});
        }       
    }
}
